Mixes up the order of the rows of target, in place. Each row moves as a whole, so the cells of a row stay together and in the same order left to right; only which row comes first, second and so on changes. It makes no copy.
grid_shuffle_rows(target)| Name | Type |
|---|---|
target | grid |
Nothing.
Mixes up the order of the rows of target, a grid. Each row moves as a whole, so the cells of a row stay together and stay in the same order from left to right. Only which row comes first, second, third and so on changes.
Before After grid_shuffle_rows(table)
1 50 3 70
2 90 1 50
3 70 2 90That is what to use for a table where each row means something as a whole, like a player's number and their score: the scores stay beside the right players. To mix up every cell on its own instead, use grid_shuffle. To move whole columns, use grid_shuffle_columns.
grid_shuffle_rows moves the values that are already in target. It does not make a new grid, and it needs no extra memory. The old order of the rows is gone afterwards. If you want to keep it, make a copy with grid_copy first. The grid keeps its width and height.
Any kind of value can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the grid holds.
A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:
roll_reseed changes it. Calling roll_reseed with a number starts the stream again from that number, so roll_reseed(7) just before grid_shuffle_rows gives the same shuffle every time you do it with the rows in the same order to start with. For a different shuffle each game, reseed from something the player decides (see roll_reseed).roll_int, roll_real and roll_chance that comes after it.Every possible order of the rows can come up. It is very slightly more likely to land on some than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.
To find a row, GBML needs to know how many cells are in it. A grid only knows its width and height where it was made (grid_create(8, 6, 0), /// @capacity 8x6, or grid<int, 8, 6>). A grid with no size written like that, or a script's grid parameter, has none to give, and the call is refused, the same as grid_width is. In a script, grid_shuffle still works on the whole grid.
| You do this | What happens |
|---|---|
| shuffle a grid with one row | nothing changes, and no random numbers are used |
| shuffle rows that are the same as each other | works as normal. You cannot tell which is which |
| shuffle and hope a row moves | it may stay where it was. That can happen with random order |
| give it a list or an array | refused when checked |
| use it where a value is needed | refused when checked. It gives nothing back |
Nothing is set aside: the rows are swapped where they stand, one row swap (a swap of every cell in the two rows) for each row after the first. No timing was measured.
var table = grid_create(2, 3, 0);
table[# 0, 0] = 1;
table[# 1, 0] = 50;
table[# 0, 1] = 2;
table[# 1, 1] = 90;
table[# 0, 2] = 3;
table[# 1, 2] = 70;
grid_shuffle_rows(table); // the three rows in a new order, each row unchangedA Fisher-Yates shuffle over the row numbers, where swapping two rows swaps their width cells one by one. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each row after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled. It needs the grid's width and height, which the checker reads from the declaration and writes into the C++ as plain numbers; a grid with none, such as a script's parameter, is refused.